package com.gange.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gange.entity.Video;
import com.gange.service.IVideoService;
import com.gange.utils.JSONUtils;
import com.gange.utils.VideoUtils;
import com.gange.vo.VideoDetailVO;
import com.gange.vo.VideoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 视频 前端控制器
 * </p>
 *
 * @author gange
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/video")
@Slf4j
public class VideoController {
    @Autowired
    private IVideoService videoService;
    @Autowired
    private VideoUtils videoUtils;


    /**
     * 视频推荐
     */
    @GetMapping("recommends") //video
    public List<VideoVO> recommends(@RequestParam("page") Integer page, @RequestParam("per_page") Integer per_page) {
        IPage<Video> videoPage = videoService.page(new Page<Video>(page, per_page));
        log.info("视频推荐列表数量: {}", videoPage.getSize());
        return videoUtils.VideoListToVideoVoList(videoPage.getRecords());
    }

    /**
     * 查看分类下视频列表
     */
    @GetMapping("/videos")
    public List<VideoVO> videos(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                @RequestParam("category") Integer categoryId) {
        log.info("当前页:{},每页显示记录数:{},分类id:{}", page, rows, categoryId);
        Page<Video> pageInfo = new Page<>(page,rows);
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",categoryId);
        IPage<Video> videoPage = videoService.page(pageInfo, wrapper);
        log.info("复合条件的记录总数: {}", videoPage.getSize());
        return videoUtils.VideoListToVideoVoList(videoPage.getRecords());
    }


    /**
     * 查询视频详情
     */
    @GetMapping("/videos/{id}")
    public VideoDetailVO video(@PathVariable("id") String videoId, String token) {
        log.info("当前接收到的videoId: {}", videoId);
        VideoDetailVO videoDetailVO = videoService.queryDetailById(videoId, token);
        log.info("查询到的视频详细对象信息: {}", JSONUtils.writeValueAsString(videoDetailVO));
        return videoDetailVO;
    }

}

